package com.mobile.service.impl;

import com.github.pagehelper.PageInfo;
import com.mobile.service.convert.BusinessDocumentConverter;
import com.mobile.service.dao.DaoSupport;
import com.mobile.service.dto.BusinessDocumentDTO;
import com.mobile.service.dto.BusinessDocumentQueryDTO;
import com.mobile.service.dto.WorkOrderDTO;
import com.mobile.service.entity.BusinessDocument;
import com.mobile.service.inter.BusinessDocumentService;
import com.mobile.service.mapper.BusinessDocumentMapper;
import com.mobile.service.mapper.WorkOrderMapper;
import com.mobile.service.util.PageUtils;
import com.mobile.util.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Objects;

/**
 * 业务文书表service实现类
 */
@Service("businessDocumentService")
@Transactional
public class BusinessDocumentServiceImpl implements BusinessDocumentService {
    @Resource
    private BusinessDocumentMapper businessDocumentMapper;
    @Autowired
    private BusinessDocumentConverter businessDocumentConverter;

    @Autowired
    private DocumentGenerationService documentGenerationService;

    @Autowired
    private WorkOrderMapper workOrderMapper;

    @Override
    public Map<String, Object> addBusinessDocument(BusinessDocumentDTO dto)  {
        Map<String, Object> map = new HashMap<>();
            businessDocumentMapper.insertBusinessDocument(businessDocumentConverter.toEntity(dto));
            map.put(Const.ACTION_RESULT, Const.ACTION_RESULT_SUCCESS);
            map.put(Const.ACTION_MESSAGE, MessageUtil.getMessage("I0011"));
        return map;
    }

    @Override
    public PageResult<BusinessDocumentDTO> getBusinessDocumentListPage(BusinessDocumentQueryDTO queryDTO)  {
        PageUtils.startPage(queryDTO);
        List<BusinessDocumentDTO> list = businessDocumentMapper.businessDocumentlistPage(queryDTO);
        return new PageResult<BusinessDocumentDTO>(list);
    }

    @Override
    public BusinessDocumentDTO getBusinessDocumentById(Integer id) {
        return businessDocumentMapper.getBusinessDocumentById(id);
    }

    @Override
    public Map<String, Object> updateBusinessDocument(BusinessDocumentDTO dto) {
        Map<String, Object> map = new HashMap<>();
        Integer pkValue = dto.getDocumentId();
        if (pkValue == null) {
            map.put(Const.ACTION_RESULT, Const.ACTION_RESULT_FAILED);
            map.put(Const.ACTION_MESSAGE, MessageUtil.getMessage("E0001"));
            return map;
        }

        BusinessDocumentDTO data = this.getBusinessDocumentById(pkValue);
        if (Objects.isNull(data)) {
            map.put(Const.ACTION_RESULT, Const.ACTION_RESULT_FAILED);
            map.put(Const.ACTION_MESSAGE, MessageUtil.getMessage("E0001"));
            return map;
        }

        businessDocumentMapper.updateBusinessDocument(businessDocumentConverter.toEntity(dto));
        map.put(Const.ACTION_RESULT, Const.ACTION_RESULT_SUCCESS);
        map.put(Const.ACTION_MESSAGE, MessageUtil.getMessage("I0009"));
        return map;
    }

    @Override
    public Map<String, Object> removeBusinessDocument(Integer id){
        Map<String, Object> map = new HashMap<>();
        businessDocumentMapper.deleteBusinessDocument(id);
        map.put(Const.ACTION_RESULT, Const.ACTION_RESULT_SUCCESS);
        map.put(Const.ACTION_MESSAGE, MessageUtil.getMessage("I0010"));
        return map;
    }

    @Override
    public List<BusinessDocumentDTO> getBusinessDocumentList(BusinessDocumentDTO dto)  {
        return businessDocumentMapper.getBusinessDocumentList(dto);
    }

    @Override
    public void generateAndSaveBusinessDocument(Integer workOrderId) {
        WorkOrderDTO workOrderDTO = workOrderMapper.getWorkOrderById(workOrderId);
        documentGenerationService.generateAndSaveBusinessDocument(workOrderDTO);
    }
}
